package LeetCode.exercise;

import java.util.ArrayList;
import java.util.List;

public class LC438 {
    public static void main(String[] args) {
        LC438 l = new LC438();
        System.out.println(l.findAnagrams("abab", "ab"));
    }
    public List<Integer> findAnagrams(String s, String p) {
        // HashMap<Character, Integer> hashMap1 = new HashMap<>();
        // HashMap<Character, Integer> hashMap2 = new HashMap<>();
        int[] arr1 = new int[123];
        int[] arr2 = new int[123];
        List<Integer> List = new ArrayList<>();
        for (int i = 0; i < p.length(); i++) {
            arr1[p.charAt(i)]++;
        }
        for (int left = 0, right = 0; right < s.length(); right++) {
            arr2[s.charAt(right)]++;
            if (right - left + 1 > p.length()) {
                arr2[s.charAt(left)]--;
                left++;
            }
            if (right - left + 1 == p.length()) {
                if (isEquals(arr1, arr2)) {
                    List.add(left);
                }
            }
        }
        return List;
    }

    public boolean isEquals(int[] arr1, int[] arr2) {
        for (int i = 97; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }
}
